package org.ecloud.base.monitor.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.ecloud.base.monitor.utils.JvmUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Scope("prototype")
public class JvmAop {

	@Autowired
	private Environment env;
	
	@Pointcut("execution(public * org.ecloud..*.provider..*.*(..))")
	public void beforeCut(){}
	@Pointcut("execution(public * org.ecloud..*.provider..*.*(..))")
	public void afterCut(){}
	
	@Before("beforeCut()")
	public void doBefore(JoinPoint joinPoint) {
		if(!isOpen()) return;
		JvmUtil.jvm();
	}
	
	@After("afterCut()")
	public void doAfter(JoinPoint joinPoint) {
		if(!isOpen()) return;
		JvmUtil.jvm();
	}
	
	private boolean isOpen(){
		String open = env.getProperty("aop.jvm.open", "false");
		
		return Boolean.valueOf(open);
	}
	
}
